package com.tanhua.sso.controller;

import com.tanhua.sso.service.MyCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改用户个人信息
 *
 * @author zZZFf
 * @since 2021/7/11 10:01
 */
@RestController
@RequestMapping("users")
public class MyCenterController {
    @Autowired
    private MyCenterService myCenterService;

    /**
     * 修改用户个人头像
     *
     * @param token     令牌
     * @param headPhoto 头像文件
     * @return
     */
    @PostMapping("header")
    public ResponseEntity<Void> updateUserLogo(@RequestParam(name = "Authorization") String token,
                                               @RequestParam(name = "headPhoto") MultipartFile headPhoto) {
        try {
            Boolean bool = this.myCenterService.updateUserLogo(headPhoto, token);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 更改手机号
     * 第一步：发送短信验证码
     */
    @PostMapping("phone/sendVerificationCode")
    public ResponseEntity<Void> SendVerificationCode(@RequestHeader(name = "Authorization") String token) {
        try {
            Boolean bool = this.myCenterService.sendVerificationCode(token);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 更改手机号
     * 第二步：校验验证码
     *
     * @param token 令牌
     * @param map   存储要检验的验证码
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity<Map<String, Boolean>> checkVerificationCode(@RequestHeader(name = "Authorization") String token,
                                                                      @RequestBody() Map<String, String> map) {
        try {
            // 验证码
            String verificationCode = map.get("verificationCode");
            Boolean result = this.myCenterService.checkVerificationCode(token, verificationCode);
            Map<String, Boolean> resultMap = new HashMap<>();
            resultMap.put("verification", result);
            if (result) {
                return ResponseEntity.ok(resultMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 更改手机号
     * 第三步：保存手机号
     *
     * @param token 令牌
     * @param map   存储要保存的新手机号
     */
    @PostMapping("phone")
    public ResponseEntity<Void> saveNewPhoneNumber(@RequestHeader(name = "Authorization") String token,
                                                   @RequestBody() Map<String, String> map) {
        try {
            String newPhoneNumber = map.get("phone");
            Boolean bool = this.myCenterService.saveNewPhoneNumber(token, newPhoneNumber);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
